
/* EXAMPLE */
/*
$('myform').getElements('input[type=file]').forEach( function(el) {new Fileupload(el); });
*/

var Fileupload = new Class({
    Implements: Options,

    initialize: function(element, options) {
        this.setOptions(options);
        this.element = element
        this.element.addEvent('change', this.fselected.bind(this));

        this.fileinfo = new Element('div',{'class':'filesinfo'});
        this.fileinfo.inject(this.element,'after');

    },

    fselected: function() {
        var files = this.element.files;

        this.fileinfo.getChildren('.fileinfo').destroy();

        for (i=0;i<files.length;i++) {
            var file=files[i];
            var fileSize = 0;
            if (file.size > 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
            else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

            var filetype = file.type || 'application/octet-stream';

            var textinfo = '<div><b>Name:</b> ' + file.name + '</div><div><b>Size:</b> ' + fileSize + '</div><div><b>Type:</b> ' + filetype + '</div>';
            new Element('div', { 'class': 'fileinfo', 'html':textinfo }).inject(this.fileinfo);
        }

    }
});
